package org.xdj.blue.java.io.server;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * socket服务端线程
 * @author 91power—xdj
 *
 */
public class MyServerSocket implements Runnable {
	
	public static Logger logger = LoggerFactory.getLogger(MyServerSocket.class);
	private int port;
	private ServerSocket serverSocket;
	private ExecutorService executorService;
	public MyServerSocket(int port) {
		this.port = port;
		try {
			serverSocket = new ServerSocket(port);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		executorService = Executors.newFixedThreadPool(50);
	}

	public void run() {
		Socket socket=null;
		while(true){
			try {
				socket = serverSocket.accept();
				logger.debug("--获得到一个连接--");
				//new Thread(new SocketHandle(socket)).start();	//BIO阻塞编程IO
				executorService.execute(new SocketHandle(socket)); //伪异步IO
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
